package com.infinitus.multids.service.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.infinitus.multids.service.RedisService;

/**
 * redicache 工具类
 * 
 */
@SuppressWarnings("unchecked")
@Service
public class RedisServiceImpl implements RedisService {
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#remove(java.lang.String)
	 */
	@Override
	public void remove(final String... keys) {
		for (String key : keys) {
			remove(key);
		}
	}

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#removePattern(java.lang.String)
	 */
	@Override
	public Set<Serializable> keys(final String pattern) {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		return keys;
	}
	
	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#removePattern(java.lang.String)
	 */
	@Override
	public void removePattern(final String pattern) {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#remove(java.lang.String)
	 */
	@Override
	public void remove(final String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#exists(java.lang.String)
	 */
	@Override
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#get(java.lang.String)
	 */
	@Override
	public String get(final String key) {
		String result = null;
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		result = (String) operations.get(key);
		return result;
	}

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#set(java.lang.String, java.lang.Object)
	 */
	@Override
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.infinitus.CatClean.service.RedisService#set(java.lang.String, java.lang.Object, java.lang.Long)
	 */
	@Override
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.MILLISECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public boolean sadd(final String key, Object... values) {
		boolean result = false;
		try {
			SetOperations<String, Object> set = redisTemplate.opsForSet();
			set.add(key, values);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	@Override
	public boolean lpush(final String key, Object value) {
		boolean result = false;
		try {
			ListOperations<String, Object> list = redisTemplate.opsForList();
			list.leftPush(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public boolean rpush(final String key, Object value) {
		boolean result = false;
		try {
			ListOperations<String, Object> list = redisTemplate.opsForList();
			list.rightPush(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public Set<Object> smembers(final String key) {
		SetOperations<String, Object> set = redisTemplate.opsForSet();
		return set.members(key);
	}
	
	@Override
	public boolean contains(final String key, Object value) {
		boolean result = false;
		try {
			SetOperations<String, Object> set = redisTemplate.opsForSet();
			result = set.isMember(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public boolean expire(final String key, Long expireTime) {
		boolean result = false;
		try {
			redisTemplate.expire(key, expireTime, TimeUnit.MILLISECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
		Set<Serializable> set = new HashSet<Serializable>();
		set.add("asdsd");
		byte[] bs = new byte[]{'s'};
		set.add(bs);
		for(Serializable kk : set){
			String s = (String)kk;
			System.out.println(s);
		}
	}
}